package extensions;

import static util.FileUtil.*;
import static util.PropertiesReader.*;
import static util.Properties.Dirs.*;
import static util.Properties.Packages.*;

import java.util.*;

import TapestryModel.*;
import TapestryModel.Object;

public class CreateProject {

    public static void createProject(final Site site) throws Throwable {
        try {
            String base = _(srcGenPath) + "/" + site.getArtifactId();
            String basePackageDir = base + "/" + _(mainJavaDir) + "/" + site.getBasePackage();

            HashMap<String, String> params = getParams(site);

            initDir(base);
            initDir(base + _(mainJavaDir));            
            initDir(base + _(mainResourcesDir));
            initDir(base + _(testJavaDir));
            initDir(base + _(testResourcesDir));
            initDir(base + _(metaInfDir));
            initDir(base + _(webInfDir));
            initDir(base + _(stylesDir));
            initDir(base + _(libDir));

            initDir(basePackageDir + _(modelPackage));
            initDir(basePackageDir + _(modelBasePackage));
            
            initDir(basePackageDir + _(pagesPackage));
            initDir(basePackageDir + _(basePagesPackage));
            initDir(basePackageDir + _(servicesPackage));
            initDir(basePackageDir + _(serverPackage));
            initDir(basePackageDir + _(serverBasePackage));

            copyFile(params, "pom.xml",          base);
            copyFile(params, "build.xml",        base);
            copyFile(params, "log4j.properties", base + _(mainResourcesDir));
            copyFile(params, "web.xml",          base + _(webInfDir));
            copyFile(params, "app.properties",   base + _(webInfDir));
            copyFile(params, "favicon.ico",      base + _(webappDir));
            copyFile(params, "MANIFEST.MF",      base + _(metaInfDir));
            copyFile(params, "styles.css",       base + _(stylesDir));
            
            copyFile(params, "generated",        basePackageDir + _(modelBasePackage));
            copyFile(params, "generated",        basePackageDir + _(basePagesPackage));
            copyFile(params, "generated",        basePackageDir + _(serverBasePackage));
            
            copyFile(params, "java/AbstractBasePage",         "AbstractBasePage.java",         basePackageDir + "/" + _(basePagesPackage));
            copyFile(params, "java/AppModule",                "AppModule.java",                basePackageDir + _(servicesPackage));
            copyFile(params, "java/ServerFacadeProvider",     "ServerFacadeProvider.java",     basePackageDir + _(servicesPackage));
            copyFile(params, "java/ServerFacadeProviderImpl", "ServerFacadeProviderImpl.java", basePackageDir + _(servicesPackage));
            
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
    }

    private static HashMap<String, String> getParams(final Site site) throws Throwable {
        try {
            String servicePackageDeclaration = (site.getBasePackage() + _(servicesPackage)).replaceAll("/", ".");
            String serverPackageDeclaration = (site.getBasePackage() + _(serverPackage)).replaceAll("/", ".");
            String modelPackageDeclaration = (site.getBasePackage() + _(modelPackage)).replaceAll("/", ".");
            String pageBasePackageDeclaration = (site.getBasePackage() + _(basePagesPackage)).replaceAll("/", ".");

            HashMap<String, String> params = new HashMap<String, String>();

            params.put("#artifactId", site.getArtifactId());
            params.put("#groupId", site.getGroupId());
            params.put("#name", site.getName());
            params.put("#basePackage", site.getBasePackage());
            params.put("#basePackPath", site.getBasePackage().replaceAll("\\.", "/"));
            params.put("#description", site.getDescription());
            params.put("#version", site.getVersion());
            params.put("#tapestry-release-version", site.getTapestryVersion());
            params.put("#servicePackage", servicePackageDeclaration);
            params.put("#serverPackage", serverPackageDeclaration);
            params.put("#modelPackage", modelPackageDeclaration);
            params.put("#pageBasePackage", pageBasePackageDeclaration);
            params.put("#mainJavaDir", _(mainJavaDir).substring(1));
            params.put("#testJavaDir", _(testJavaDir).substring(1));
            params.put("#mainWebAppDir", _(webappDir).substring(1));
            params.put("#targetClasses", _(targetClasses).substring(1));
            params.put("#targetTestClasses", _(targetTestClasses).substring(1));

            for (Object object : site.getObject()){
                if(object instanceof Entity)

                if (((Entity)object).isSession())
                    params.put("#sessionClassName", object.getName());
            }
            return params;

        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
    }
}
